local shensu = fk.CreateSkill {

  name = "joyex__shensu",

  tags = {  },

}



shensu:addEffect(fk.EventPhaseChanging, {
  name = "joyex__shensu",
  anim_type = "offensive",
  events = {fk.EventPhaseChanging},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(shensu.name) and not player:prohibitUse(Fk:cloneCard("thunder__slash")) then
      if (data.to == Player.Judge) or data.to == Player.Discard then
        return true
      elseif data.to == Player.Play then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local slash = Fk:cloneCard("thunder__slash")
    local max_num = slash.skill:getMaxTargetNum(player, slash)
    local targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not player:isProhibited(p, slash) then
        table.insert(targets, p.id)
      end
    end
    if #targets == 0 or max_num == 0 then return end
    if data.to == Player.Judge then
      local tos = room:askForChoosePlayers(player, targets, 1, max_num, "#joyex__shensu1-choose", shensu.name, true)
      if #tos > 0 then
        event:setCostData(self, {tos})
        return true
      end
    elseif data.to == Player.Play then
      local tos = room:askForChoosePlayers(player, targets, 1, max_num, "#joyex__shensu2-choose", shensu.name, true)
      if #tos > 0  then
        event:setCostData(self, {tos})
        return true
      end
    elseif data.to == Player.Discard then
      local tos = room:askForChoosePlayers(player, targets, 1, max_num, "#joyex__shensu3-choose", shensu.name, true)
      if #tos > 0 then
        event:setCostData(self, {tos})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.to == Player.Judge then
      player:skip(Player.Judge)
      player:skip(Player.Draw)
    elseif data.to == Player.Play then
      player:skip(Player.Play)
    elseif data.to == Player.Discard then
      player:skip(Player.Discard)
      player:turnOver()
    end
    local slash = Fk:cloneCard("thunder__slash")
    slash.skillName = shensu.name
    room:useCard({
      from = target.id,
      tos = table.map(event:getCostData(self)[1], function(pid) return { pid } end),
      card = slash,
      extraUse = true,
    })
    return true
  end,
})

return shensu